import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../store/user'
import { ElMessageBox } from 'element-plus'

// 路由懒加载
const Login = () => import('../views/Login.vue')
const Register = () => import('../views/Register.vue')
const Layout = () => import('../views/Layout.vue')
const Home = () => import('../views/Home.vue')
const CarBrowse = () => import('../views/CarBrowse.vue')
const SalesStatistics = () => import('../views/SalesStatistics.vue')
const SalesRanking = () => import('../views/SalesRanking.vue')
const CarManagement = () => import('../views/CarManagement.vue')
const AIAssistant = () => import('../views/AIAssistant.vue')
const Profile = () => import('../views/Profile.vue')

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'Home',
        component: Home,
        meta: { title: '首页', requiresAuth: false } // 首页不需要认证
      },
      {
        path: 'car-browse',
        name: 'CarBrowse',
        component: CarBrowse,
        meta: { title: '汽车浏览', requiresAuth: false } // 汽车浏览不需要认证
      },
      {
        path: 'sales-statistics',
        name: 'SalesStatistics',
        component: SalesStatistics,
        meta: { title: '销售统计', requiresAuth: true } // 需要认证
      },
      {
        path: 'sales-ranking',
        name: 'SalesRanking',
        component: SalesRanking,
        meta: { title: '销售排行', requiresAuth: true } // 需要认证
      },
      {
        path: 'car-management',
        name: 'CarManagement',
        component: CarManagement,
        meta: { title: '汽车管理', requiresAuth: true } // 需要认证
      },
      {
        path: 'ai-assistant',
        name: 'AIAssistant',
        component: AIAssistant,
        meta: { title: 'AI助手', requiresAuth: true } // 需要认证
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile,
        meta: { title: '个人中心', requiresAuth: true } // 需要认证
      }
    ]
  },
  // 将未匹配的路由重定向到首页
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  const isAuthenticated = userStore.isLoggedIn
  
  // 对于需要认证的路由
  if (to.meta.requiresAuth === true && !isAuthenticated) {
    try {
      // 弹出确认框询问用户是否要登录
      await ElMessageBox.confirm(
        '您需要登录后才能访问该页面，是否前往登录？',
        '提示',
        {
          confirmButtonText: '去登录',
          cancelButtonText: '回首页',
          type: 'warning',
          distinguishCancelAndClose: true
        }
      )
      // 用户选择去登录
      next({ name: 'Login', query: { redirect: to.fullPath } })
    } catch (action) {
      // 用户选择取消或关闭对话框
      if (action === 'cancel' || action === 'close') {
        // 回到首页
        next({ name: 'Home' })
      }
    }
  } else {
    next()
  }
})

export default router